<template>
    <el-card>
        <div slot="header">
            <span>店铺管理</span>
            <el-button style="float: right; padding: 10px 10px;" type="primary" @click="submit">保存</el-button>
        </div>

        <!-- 表单 -->
        <el-form ref="form" :model="form" label-width="80px" style="width:450px">
            <el-form-item label="店铺名称">
                <el-input v-model="form.name"></el-input>
            </el-form-item>

            <el-form-item label="店铺公告">
                <el-input type="textarea" v-model="form.bulletin"></el-input>
            </el-form-item>

			<el-form-item label="店铺头像">
                <el-upload
					class="avatar-uploader"
					action="http://sell.h5.itsource.cn:8087/shop/upload"
					:show-file-list="false"
					:on-success="handleAvatarSuccess"
					:before-upload="beforeAvatarUpload">
					<img v-if="form.avatar" :src="form.avatar" class="avatar">
					<i v-else class="el-icon-plus avatar-uploader-icon"></i>
				</el-upload>
            </el-form-item>

            <el-form-item label="店铺图片">
                <el-upload
					action="http://sell.h5.itsource.cn:8087/shop/upload"
					:file-list="fileList"
					list-type="picture-card"
					:on-preview="handlePictureCardPreview"
					:on-remove="handleRemove">
					<i class="el-icon-plus"></i>
				</el-upload>
				<el-dialog :visible.sync="dialogVisible">
					<img width="100px" :src="dialogImageUrl" alt="">
				</el-dialog>
            </el-form-item>

            <el-form-item label="配送费">
                <el-input v-model="form.deliveryPrice"></el-input>
            </el-form-item>

            <el-form-item label="配送时间">
                <el-input v-model="form.deliveryTime"></el-input>
            </el-form-item>

            <el-form-item label="配送描述">
                <el-input v-model="form.description"></el-input>
            </el-form-item>

            <el-form-item label="店铺评分">
                <el-input v-model="form.score"></el-input>
            </el-form-item>

            <el-form-item label="销量">
                <el-input v-model="form.sellCount"></el-input>
            </el-form-item>

            <el-form-item label="活动">
                <template>
					<el-checkbox-group v-model="form.supports">
						<el-checkbox label="在线支付满28减5"></el-checkbox>
						<el-checkbox label="VC无限橙果汁全场8折"></el-checkbox>
						<el-checkbox label="单人精彩套餐"></el-checkbox>
						<el-checkbox label="特价饮品8折抢购"></el-checkbox>
						<el-checkbox label="单人特色套餐"></el-checkbox>
					</el-checkbox-group>
              </template>
            </el-form-item>

            <el-form-item label="营业时间">
				<el-time-picker
					is-range
					v-model="form.date"
					range-separator="至"
					start-placeholder="开始时间"
					end-placeholder="结束时间"
					placeholder="选择时间范围">
				</el-time-picker>
            </el-form-item>
        </el-form>
    </el-card> 
</template>

<script>
import {shopInfo, shopEdit, shopUpload} from "@/api/shop.js";  //引入店铺详情
export default {
	data() {
		return {
		form: {
			name: '',
			bulletin: '',
			avatar: '',
			deliveryPrice: '',
			deliveryTime: '',
			description: "",
			score: "",
			sellCount: '',
			date: [],
			supports: [],
			pics:[],
		},
		// 营业时间
		value1: [new Date(2016, 9, 10, 8, 40), new Date(2016, 9, 10, 9, 40)],

		// 多选框
		checkList: ['选中且禁用','复选框 A'],

		// 店铺图片
		fileList: [],
		dialogImageUrl: '',
		dialogVisible: false,
		}
	},

	async created(){
		let res = await shopInfo();
		this.form = res.data.data;
		this.fileList = res.data.data.pics.map(v=>{
			return {name:123, url:v}
		})
	},

	methods: {
		// 保存按钮
		async submit() {
			// this.form.pics = JSON.stringify(this.form.pics)
			let res = await shopEdit(this.form,{pics: this.fileList});
			console.log(res)

		},

		// 店铺图片
		handleRemove(file, fileList) {
			console.log(file, fileList);
		},
		handlePictureCardPreview(file) {
			this.dialogImageUrl = file.url;
			this.dialogVisible = true;
		},
		
		// 店铺头像
		handleAvatarSuccess(res, file) {
			this.form.avatar = URL.createObjectURL(file.raw);
		},
		beforeAvatarUpload(file) {
			const isJPG = file.type === 'image/jpeg';
			const isLt2M = file.size / 1024 / 1024 < 2;

			if (!isJPG) {
			this.$message.error('上传头像图片只能是 JPG 格式!');
			}
			if (!isLt2M) {
			this.$message.error('上传头像图片大小不能超过 2MB!');
			}
			return isJPG && isLt2M;
		}
	}
}
</script>

<style lang="less" scoped>
:deep(.avatar-uploader .el-upload) {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>